package com.repair.sms.config;

import com.repair.sms.service.SmsService;
import com.repair.sms.service.impl.MockSmsServiceImpl;
import com.repair.sms.service.impl.SmsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chunyu
 * @since 2024/9/18,19:38
 */
@Configuration
@EnableConfigurationProperties(SmsProperties.class)
public class SmsConfig {

    @Autowired
    private SmsProperties smsProperties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = SmsProperties.PREFIX, value = "enabled", havingValue = "true")
    public SmsService smsService() {
        SmsServiceImpl smsService = new SmsServiceImpl();
        smsService.setEndpoint(smsProperties.getEndpoint());
        smsService.setAccessKeyId(smsProperties.getAccessKeyId());
        smsService.setAccessKeySecret(smsProperties.getAccessKeySecret());
        smsService.setSignName(smsProperties.getSignName());
        smsService.setTemplateCode(smsProperties.getTemplateCode());
        return smsService;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = SmsProperties.PREFIX, value = "enabled", havingValue = "false")
    public SmsService mockSmsService() {
        return new MockSmsServiceImpl();
    }

}
